/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.client.disposer.gui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import org.toools.gwt.widget.client.bc.BasicControl;
import org.toools.gwt.widget.client.bc.BasicListRenderer;
import org.toools.gwt.widget.client.bc.BasicTogglePanel;
import org.toools.gwt.widget.client.bc.DefaultCallback;
import org.toools.gwt.widget.client.bc.DefaultDeleteTogglePanel;
import org.toools.gwt.widget.client.bc.RelationControl;
import org.toools.gwt.widget.client.cloud.CloudListener;
import org.toools.gwt.widget.client.cloud.TagVO;
import org.toools.gwt.wmc.client.container.ContainerService;
import org.toools.gwt.wmc.client.container.ContainerServiceAsync;
import org.toools.gwt.wmc.client.container.gui.ContainerControl;
import org.toools.gwt.wmc.client.disposer.DisposerServiceAsync;
import org.toools.gwt.wmc.jpa.Container;
import org.toools.gwt.wmc.jpa.Disposer;
import org.toools.gwt.wmc.jpa.Waste;

/**
 *
 * @author Manuel
 */
public class DisposerListRenderer extends BasicListRenderer{

    private ContainerControl cc = null;

    private Disposer disp = null;

    org.toools.gwt.widget.client.bc.ToggleButton tb = new org.toools.gwt.widget.client.bc.ToggleButton("containers", false) {
        public void toggle() {
            super.toggle();
            if(cc != null) {
                cc.setVisible(isOn());
            }
        }
    };

    BasicTogglePanel containers = new BasicTogglePanel("set containers", 1, true) {
        public void onActive() {
            clear();
            addChild(new ContainerRelation((Disposer)getTag().getAttachedBusinessObject()));
        }
    };

    public DisposerListRenderer(TagVO tag, BasicControl bc) {
		super(tag, bc);
        disp = (Disposer) tag.getAttachedBusinessObject();
        cc = new ContainerControl(ContainerService.Util.getInstance(),(Disposer)tag.getAttachedBusinessObject());
        cc.setVisible(false);
        addLeft(tb);
        addControl(cc);
        addButton( containers );
	}

    @Override
    public void setTag(TagVO tag) {
        Disposer ac = (Disposer) tag.getAttachedBusinessObject();
		setTitle(ac.getName());
    }

    public DefaultDeleteTogglePanel getDefaultDeleteTogglePanel() {
        return new DefaultDeleteTogglePanel(getBasicControl(), getTag().getAttachedBusinessObject()) {
            public boolean valid() {
                Disposer bo = (Disposer) getTag().getAttachedBusinessObject();
                if( (bo.getContainers() != null) && (bo.getContainers().size() > 0) )  {
                    return false;
                } else {
                    return true;
                }
            }
            public String getDeleteConfirmMessage() {
                if(!valid())  {
                    return "delete containers first.";
                } else {
                    return getBasicControl().getDeleteConfirmMessage();
                }
            }
        };
    }

    public class ContainerRelation extends RelationControl {
        Disposer disposer;
        public ContainerRelation(Disposer disp) {
            super(false, true, true, false);
            this.disposer = disp;
            ContainerService.Util.getInstance().getContainers(loadCallback);
            this.getRelationList().addCloudListener(updateCloudListener);
        }
        public TagVO getTagVO(Object bp) {
            Container container = (Container) bp;
            TagVO result = new TagVO("" + container.getId(), container.getName(), 0, 0);
            result.setAttachedBusinessObject(container);
            return result;
        }
        AsyncCallback loadCallback = new DefaultCallback() {
            public void onSuccess(Object result) {
                super.onSuccess(result);
                setTags(getTagVO(((Vector<Container>)result).toArray()));
                if((disposer.getContainers() != null) && (disposer.getContainers().size() > 0)) {
                    String[] active = new String[disposer.getContainers().size()];
                    int j = 0;
                    for(Iterator i = disposer.getContainers().iterator(); i.hasNext();) {
                        Container container = (Container) i.next();
                        active[j++] = "" + container.getId();
                    }
                    getRelationList().setActive(active);
                }
                getStatus().setText("");
            }
        };
        CloudListener updateCloudListener = new CloudListener() {
            public void onSelectionChanged(java.util.Iterator tags) {
                Collection<Container> containers = new Vector<Container>();
                for(; tags.hasNext();) {
                    TagVO tag = (TagVO) tags.next();
                    containers.add((Container)tag.getAttachedBusinessObject());
                }
                disposer.setContainers(containers);
                ((DisposerServiceAsync)getBasicControl().getService()).update(disposer, new DefaultCallback());
            }
            public void onTagActivated(TagVO tag) {}
            public void onTagDeactivated(TagVO tag) {}
            public void onFilterChanged() {}
            public void onTagsChanged() {}
        };
    }

}
